﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Security.Cryptography;//////////////////////////////////////
using System.Security.Cryptography.X509Certificates;
using System.IO;
namespace DES
{
        public class Class1
        {

        //加密博客A
        public string Encrypt2(string PlainText, string KEY_64, string IV_64)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
            MemoryStream ms = new MemoryStream();
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cst);
            sw.Write(PlainText);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }
        //加密博客B
        public byte[] DesEncrypt(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            //return ret.ToString();

            return ms.ToArray();
            //return a;
        }





        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="entryStr">加密字节数组</param>
        /// <param name="key">8位16进制密钥字节数组</param>
        /// <returns></returns>
        public byte[] Encrypt(byte[] entryStr, byte[] key)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.Zeros;
                des.Key = key;

                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] bytes = entryStr;
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytes, 0, bytes.Count());
                        cs.FlushFinalBlock();
                    }
                    return ms.ToArray();
                }
            }


        /// <summary>
        /// DES 解密 
        /// </summary>
        /// <param name="bytes">解密加密字节数组</param>
        /// <param name="key">8位16进制密钥字节数组</param>
        /// <returns></returns>
        public  byte[] Decrypt(byte[] bytes, byte[] key)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.Zeros;
                des.Key = key;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytes, 0, bytes.Count());
                        cs.FlushFinalBlock();
                    }
                    return ms.ToArray();
                }
            }
        }

    
}
